<?php
/**
 * Pipahr
 * @brief An action to delete a attachment
 *
 * @author Chenwei
 * @copyright 2013 pipahr.com
 */
OCP\JSON::checkAppEnabled('calendar');

if(!OC_User::isLoggedIn()) {
   jexit(jsonError('您需要先登录', null, false));
}
if (!PSession::checkToken('post')) {
   jexit(jsonError('验证已失效，请重新验证', null, false));
}

$attachmentid = JRequest::getInt('attachmentid', 0);

$type = JRequest::getString('type', 'offer');

$jobid = JRequest::getInt('jobid',0);

if(!PCompanyHelper::checkJobManagerPermissions($jobid)){
   jexit(jsonError('您沒有操作权限！', null, false));
}

// if it is history attachment of  calendar,only remove its div
if($type == 'interview'){
   $attachname = JRequest::getString('attachname', '');
   if(empty($attachname)){
      jexit(jsonSuccess($attachmentid, null, null, false));
   }
}

$listmodel = new PCalendarModellistview();
$result = $listmodel->deleteAttachment($attachmentid,$type);
if (!$result) {
   jexit(jsonError($listmodel->getError(), null, false));
}

if($type == 'interview'){
   jexit(jsonSuccess($result, null, null, false));
}else{
   jexit(jsonSuccess('删除成功', null, null, false));
}